package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.UserConstants;
import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysDictType;
import com.yuehuanghun.admin.core.service.ISysDictTypeService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.web.BaseResource;
import com.yuehuanghun.mybatis.milu.data.Sort;
import com.yuehuanghun.mybatis.milu.data.Sort.Direction;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 数据字典信息
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/dict/type")
@SaCheckLogin
public class SysDictTypeController extends BaseResource {
	@Autowired
	private ISysDictTypeService dictTypeService;

	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@SaCheckPermission("system:dict:list")
	public PageDTO<SysDictType> list(SysDictType dictType) { //NOSONAR
		List<SysDictType> list = dictTypeService.getByExampleAndSort(dictType, Sort.by(Direction.DESC, SysDictType::getDictId), getPage());
		return toPage(list);
	}

	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("system:dict:export")
	public String export(SysDictType dictType) {  //NOSONAR
		List<SysDictType> list = dictTypeService.getByExample(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
		return util.exportExcel(list, "字典类型");
	}

	/**
	 * 查询字典类型详细
	 */
	@GetMapping(value = "/{dictId}")
	@SaCheckPermission("system:dict:query")
	public SysDictType getInfo(@PathVariable Long dictId) {
		return dictTypeService.getDictTypeById(dictId);
	}

	/**
	 * 新增字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("system:dict:add")
	public void add(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			throw new ValidateException("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dictTypeService.insertDictType(dict);
	}

	/**
	 * 修改字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:dict:edit")
	public void edit(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			throw new ValidateException("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dictTypeService.updateDictType(dict);
	}

	/**
	 * 删除字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
	@SaCheckPermission("system:dict:remove")
	public void remove(@PathVariable Long[] dictIds) {
		dictTypeService.deleteDictTypeByIds(dictIds);
	}

	/**
	 * 清空缓存
	 */
	@Log(title = "字典类型", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clearCache")
	@SaCheckPermission("system:dict:remove")
	public void clearCache() {
		dictTypeService.clearCache();
	}

	/**
	 * 获取字典选择框列表
	 */
	@GetMapping("/optionselect")
	public List<SysDictType>  optionselect() {
		return dictTypeService.getDictTypeAll();
	}
}
